package com.ddone.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ddone.helper.MenuHelper;
import com.ddone.mapper.SysMenuMapper;
import com.ddone.mapper.SysRoleMenuMapper;
import com.ddone.model.auth.SysMenu;
import com.ddone.model.auth.SysRoleMenu;
import com.ddone.model.vo.AssginMenuVo;
import com.ddone.service.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author ddone
 * @date 2024/3/29-21:03
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;


    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    @Override
    public List<SysMenu> findNodes() {
        List<SysMenu> menuList = this.list();
        if (CollectionUtils.isEmpty(menuList)) {
            return null;
        }
        //构建树形数据
        return MenuHelper.buildTree(menuList);
    }

    @Override
    public List<SysMenu> findMenuByRoleId(Long roleId) {
        //获取所有status为1的权限列表
        List<SysMenu> menuList = sysMenuMapper.selectList(new LambdaQueryWrapper<SysMenu>().eq(SysMenu::getStatus, 1));
        LambdaQueryWrapper<SysRoleMenu> queryWrapper = new LambdaQueryWrapper<SysRoleMenu>().eq(SysRoleMenu::getRoleId, roleId);
        List<Long> roleMenuIds = sysRoleMenuMapper
                .selectList(queryWrapper).stream()
                .map(SysRoleMenu::getMenuId)
                .collect(Collectors.toList());
        // 查看那些菜单被选中,并设置
        menuList.forEach(sysMenu -> sysMenu.setSelect(roleMenuIds.contains(sysMenu.getId())));

        // 将全部权限菜单构建成树并返回
        List<SysMenu> tree = MenuHelper.buildTree(menuList);
        return tree;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void doAssign(AssginMenuVo assignMenuVo) {
        Long roleId = assignMenuVo.getRoleId();
        // 删除已分配的权限
        sysRoleMenuMapper.delete(new LambdaQueryWrapper<SysRoleMenu>().eq(SysRoleMenu::getRoleId,roleId));

        // 变量所有已选择的权限ID,进行记录添加
        List<SysRoleMenu> roleMenuList = assignMenuVo.getMenuIdList().stream()
                .filter(Objects::nonNull)
                .map(menuId -> {
                    SysRoleMenu sysRoleMenu = new SysRoleMenu();
                    sysRoleMenu.setMenuId(menuId);
                    sysRoleMenu.setRoleId(roleId);
                    return sysRoleMenu;
                })
                .collect(Collectors.toList());

        sysRoleMenuMapper.insertBatch(roleMenuList);
    }
}
